<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class UsersController extends AdminbaseController {

      //用户信息 列表
    function users_lst(){
        $nickname = trim(I("nickname"));
        $phone = trim(I("phone"));
        $stime = I("kaishi");
        $etime = I("jieshu");

        $renzheng = I("renzheng");
//        var_dump($renzheng)  ;die;
        $qianyue = I("qianyue");
        $huiyuan = I("huiyuan");
        $p = I("p");
        $where = " 1 ";
        if(!empty($nickname)){
            $where .= " and nickname like '%{$nickname}%' ";
        }
        if(!empty($phone)){
            $where .= " and phone like '%{$phone}%' ";
        }
        if (!empty($renzheng)) {
            $where .= " and is_renzheng = {$renzheng}";
        }
        if (!empty($qianyue)) {

            $where .= " and is_qianyue = {$qianyue}";
        }
        if (!empty($huiyuan)) {

            $where .= " and is_member = {$huiyuan}";
        }


        if (!empty($stime)) {
            $time1 = strtotime($stime." "."00:00:00");
            $where .= " and {$time1} <= ctime";
        }
        if (!empty($etime)) {
            $time2 = strtotime($etime." "."23:59:59");
            $where .= " and ctime <= {$time2}";
        }

        $count =  M("account")->where($where)->count();
        if($count==0){
            $p = 0;
        }else{
            if(empty($p)){
                $p = 1;
            }
        }
        $totalpage = ceil($count/10);
        $page = $this->page($count,10);
        $sd = ($p-1)*10;
//               var_dump($stime);die;
        $data = M("account")
            ->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->order("id desc")
            ->select();
        $this->assign("data",$data);
        $this->assign("nickname",$nickname);
        $this->assign("phone",$phone);
        $this->assign("stime_kaishi",$stime);
        $this->assign("etime_jieshu",$etime);
        $this->assign("renzheng",$renzheng);
        $this->assign("qianyue",$qianyue);
        $this->assign("huiyuan",$huiyuan);

        $this->assign("page", $page->show('Admin'));
        $this->assign("p",$p);
        $this->assign("sd",$sd);
        $this->assign("totalpage",$totalpage);
        $this->display();
    }

    //冻结
    function change(){
        $id = I("id");
        $r = M("account")->where("id=$id")->find();
         if($r['is_dongjie']==0){
             $this->dongjie_message($r['phone'],$r['nickname']);
         }
        if($r["is_dongjie"]=='0'){
            $data["is_dongjie"] = 1;
        }else{
            $data["is_dongjie"] = 0;
        }
        M("account")->where("id=$id")->save($data);
        $this->success("操作成功",U("Users/users_lst"));
    }

    /*
  * 账号冻结发送短信
  * */
    function dongjie_message($phone, $nickname){
        require_once "./public/duanxin/api_demo/SmsDemo.php";
        ini_set('date.timezone', 'Asia/Shanghai');
        $demo = new \SmsDemo();
        $response = $demo->sendSms(
            "{$phone}", // 短信接收者
            "SMS_123665123", // 短信模板编号
            Array(  // 短信模板中字段的值
                "name" => "{$nickname}",
            )
        );
        $response = (array)$response;
        if ($response['Code'] == "OK") {
            return true;
        } else {
            return false;
        }
    }





}